from rest_framework import serializers
from users.models import User


class AdminSerializer(serializers.ModelSerializer):
    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = User
        # 指定那些字段生成
        fields = "__all__"
        extra_kwargs = {
            'password':
                {
                    'write_only': True
                }
        }

    def create(self, validated_data):
        # 重新调用父类方法
        user = super().create(validated_data)
        # 对父类保存的用户对象的密码加密
        user.set_password(validated_data['password'])
        user.is_staff = True
        user.save()

        return user
